<?php

/**
 * This is the model class for table "gallery".
 *
 * The followings are the available columns in table 'gallery':
 * @property integer $gal_code
 * @property string $gal_name
 * @property string $gal_date
 * @property integer $pag_code
 *
 * The followings are the available model relations:
 * @property Page $pagCode
 * @property Image[] $images
 */
class Gallery extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Gallery the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'gallery';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('gal_name,gal_description', 'required'),
			array('pag_code', 'numerical', 'integerOnly'=>true),
			array('gal_name', 'length', 'max'=>45),
			array('gal_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('gal_code, gal_name, gal_date, pag_code', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'pagCode' => array(self::BELONGS_TO, 'Page', 'pag_code'),
			'images' => array(self::HAS_MANY, 'Image', 'gal_code'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'gal_code' => 'Gal Code',
			'gal_name' => 'Gal Name',
			'gal_date' => 'Gal Date',
			'pag_code' => 'Pag Code',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('gal_code',$this->gal_code);
		$criteria->compare('gal_name',$this->gal_name,true);
		$criteria->compare('gal_date',$this->gal_date,true);
		$criteria->compare('pag_code',$this->pag_code);
		
		$page = (int)$_GET['page'];
    if($page > 0){
          $criteria->condition = 'pag_code='.$page;
      }else{
          throw new CHttpException(404, 'The requested page does not exist.');
      }


		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}